package com.fwj.authorization.security;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fwj.authorization.staff.entity.SysUser;
import com.fwj.authorization.staff.entity.SysUserRole;
import com.fwj.authorization.staff.service.SysUserRoleService;
import com.fwj.authorization.staff.service.SysUserService;
import com.fwj.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录相关控制器
 *
 * @author fwj
 * @date 2025-03-18
 */
@Slf4j
@RestController
@RequestMapping("/auth")
@RequiredArgsConstructor
@Api(tags = "登录相关控制器")
public class AuthController {

    private final SysUserService sysUserService;
    private final SysUserRoleService sysUserRoleService;
    private static final PasswordEncoder ENCODER = new BCryptPasswordEncoder();

    /**
     * 框架登录接口显式登录
     *
     * @return ResponseEntity<?>
     */
    @RequestMapping(value = "/login", method = RequestMethod.OPTIONS)
    public ResponseEntity<?> handleOptions() {
        return ResponseEntity.ok().build();
    }

    @PostMapping("/register")
    @ApiOperation(value = "用户注册")
    public R<Map<String, Object>> register(@Valid @RequestBody SysUser sysUser) {
        Map<String, Object> map = new HashMap<>();
        String pwd = sysUser.getPwd();
        String confirmPwd = sysUser.getConfirmPwd();
        if (StringUtils.isEmpty(pwd)) {
            return R.failed("密码不能为空！");
        }
        if (StringUtils.isEmpty(confirmPwd)) {
            return R.failed("确认密码不能为空！");
        }
        if (!pwd.equals(confirmPwd)) {
            return R.failed("两次密码不一致！");
        }
        sysUser.setPwd(ENCODER.encode(pwd));
        sysUserService.save(sysUser);
        SysUserRole sysUserRole = new SysUserRole();
        sysUserRole.setUsername(sysUser.getUsername());
        sysUserRole.setRoleCode("SYS_MANAGER");
        sysUserRoleService.save(sysUserRole);
        map.put("username", sysUser.getUsername());
        map.put("realName", sysUser.getRealName());
        return R.ok(map, "注册成功！");
    }

    @PostMapping("/updatePwd")
    @ApiOperation(value = "用户修改密码")
    public R<Boolean> updatePwd(@Valid @RequestBody Map<String, Object> params) {
        String oldPwd = MapUtil.getStr(params, "oldPwd");
        String pwd = MapUtil.getStr(params, "pwd");
        String confirmPwd = MapUtil.getStr(params, "confirmPwd");
        if (StringUtils.isEmpty(oldPwd)) {
            return R.failed("原密码不能为空！");
        }
        if (StringUtils.isEmpty(pwd)) {
            return R.failed("密码不能为空！");
        }
        if (StringUtils.isEmpty(confirmPwd)) {
            return R.failed("确认密码不能为空！");
        }
        if (!pwd.equals(confirmPwd)) {
            return R.failed("两次密码不一致！");
        }
        LoginUser user = JwtTokenUtil.getUser();
        sysUserService.update(null, Wrappers.<SysUser>update().lambda()
                .set(SysUser::getPwd, ENCODER.encode(pwd))
                .eq(SysUser::getUsername, user.getUsername()));
        return R.ok(Boolean.TRUE, "修改成功！");
    }
}
